
package com.uuah.server.api;

import org.apache.commons.lang.BooleanUtils;

import com.uuah.server.api.config.IUuahConfiguration;


public interface IThreadSafeAccess {
	boolean WRITE = true;
	boolean READ = false;


	void assertAccess(boolean write);

	void resetAccessControl();

	IThreadSafeAccess newThreadCopy();

	class AccessControl {
		private static boolean assertMessageAccess = true;
		private static boolean failOnMessageScribbling = true;

		static {
			String propertyValue = System
					.getProperty(IUuahConfiguration.SYSTEM_PROPERTY_PREFIX
							+ "message.assertAccess");
			if (propertyValue != null) {
				assertMessageAccess = BooleanUtils.toBoolean(propertyValue);
			}

			propertyValue = System
					.getProperty(IUuahConfiguration.SYSTEM_PROPERTY_PREFIX
							+ "disable.threadsafemessages");
			if (propertyValue != null) {
				failOnMessageScribbling = !BooleanUtils
						.toBoolean(propertyValue);
			}
		}

		public static boolean isAssertMessageAccess() {
			return assertMessageAccess;
		}

		public static void setAssertMessageAccess(boolean flag) {
			assertMessageAccess = flag;
		}

		public static boolean isFailOnMessageScribbling() {
			return failOnMessageScribbling;
		}

		public static void setFailOnMessageScribbling(boolean flag) {
			failOnMessageScribbling = flag;
		}
	}

}
